﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace FrameworkCheckerLib {
	public class FC {
		public List<string> VersionsNET( ) {
			List<string> vers = new List<string>( );

			string path = Environment.GetEnvironmentVariable("SystemRoot") + @"\Microsoft.NET\Framework\";
			if (System.IO.Directory.Exists(path)) {
				string[ ] list = System.IO.Directory.GetDirectories(path, "v*");
				if (list != null)
					foreach (string dir in list) {
						if (System.IO.File.Exists(dir + @"\mscorlib.dll") == true)
							vers.Add(FileVersionInfo.GetVersionInfo(dir + @"\mscorlib.dll").ProductVersion.ToString( ));
						else
							vers.Add(dir.Replace(path, ""));
					}
			}

			string pathx64 = Environment.GetEnvironmentVariable("SystemRoot") + @"\Microsoft.NET\Framework64\";
			if (System.IO.Directory.Exists(pathx64)) {
				string[ ] list = System.IO.Directory.GetDirectories(pathx64, "v*");
				if (list != null)
					foreach (string dir in list) {
						if (System.IO.File.Exists(dir + @"\mscorlib.dll") == true)
							vers.Add(FileVersionInfo.GetVersionInfo(dir + @"\mscorlib.dll").ProductVersion.ToString( ) + " x64");
						else
							vers.Add(dir.Replace(pathx64, "") + " x64");
					}
			}

			/*RegistryKey reg = Registry.LocalMachine;
			reg = reg.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP");
			if(reg.GetSubKeyNames() != null)
				foreach(string name in reg.GetSubKeyNames()){
					if(name.Substring(0,1) == "v"){
						RegistryKey temp = reg.OpenSubKey(name);
						bool es = false;
						foreach (string s in temp.GetValueNames( )) {
							if (s == "Version") vers.Add(temp.GetValue("Version").ToString());
						}
					
							
					}
				}*/

			return vers;
		}

		public List<string> VersionsSilverLight( ) {
			List<string> vers = new List<string>( );
			string path1 = Environment.GetEnvironmentVariable("SystemRoot") + @"\..\Program Files (x86)\Microsoft Silverlight\";
			string path2 = Environment.GetEnvironmentVariable("SystemRoot") + @"\..\Program Files\Microsoft Silverlight\";
			if (System.IO.Directory.Exists(path1) == true) {
				string[ ] list = System.IO.Directory.GetDirectories(path1, "*");
				if (list != null)
					foreach (string dir in list) {
						if (System.IO.File.Exists(dir + @"\mscorlib.dll") == true)
							vers.Add(FileVersionInfo.GetVersionInfo(dir + @"\mscorlib.dll").ProductVersion.ToString( ));
						else
							vers.Add(dir.Replace(path1, ""));
					}
			} else if (System.IO.Directory.Exists(path2) == true) {
				string[ ] list = System.IO.Directory.GetDirectories(path2, "*");
				if (list != null)
					foreach (string dir in list) {
						if (System.IO.File.Exists(dir + @"\mscorlib.dll") == true)
							vers.Add(FileVersionInfo.GetVersionInfo(dir + @"\mscorlib.dll").ProductVersion.ToString( ));
						else
							vers.Add(dir.Replace(path2, ""));
					}
			}
			return vers;
		}

		public List<string> VersionsDirectX( ) {
			List<string> vers = new List<string>( );
			string path = Environment.GetEnvironmentVariable("SystemRoot") + @"\System32\";
			if (System.IO.Directory.Exists(path)) {
				Regex rgx = new Regex(@"d3d(\d+)*_?(\d+)*.dll");
				string[ ] list = System.IO.Directory.GetFiles(path, "d3d*.dll");
				if (list != null)
					foreach (string file in list) {
						if (rgx.IsMatch(file) == true)
							vers.Add(rgx.Match(file).ToString( ).Replace("_", " SP ").Replace("d3d", "").Replace(".dll", ""));
					}
			}
			vers.Sort( );
			return vers;
		}

		public List<string> VersionsXNA( ) {
			List<string> vers = new List<string>( );
			List<string> dlls = new List<string>( );
			dlls.Add("Microsoft.Xna.Framework");
			dlls.Add("Microsoft.Xna.Framework.Game");

			string path1 = Environment.GetEnvironmentVariable("SystemRoot") + @"\Microsoft.NET\assembly\GAC_32\Microsoft.Xna.Framework.Game\";
			string path2 = Environment.GetEnvironmentVariable("SystemRoot") + @"\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Xna.Framework.Game\";
			if (System.IO.Directory.Exists(path1) == true) {
				string[ ] list = System.IO.Directory.GetDirectories(path1, "*");
				if (list != null)
					foreach (string dir in list) {
						foreach (string dll in dlls) {
							string versionDir = dir.Replace(path1, "");
							if (System.IO.File.Exists(dir + @"\" + dll + ".dll") == true) {
								if (vers.Contains(versionDir))
									vers.Remove(versionDir);
								vers.Add(FileVersionInfo.GetVersionInfo(dir + @"\" + dll + ".dll").ProductVersion.ToString( ));
							} else
								vers.Add(versionDir);
						}
					}
			}
			if (System.IO.Directory.Exists(path2) == true) {
				string[ ] list = System.IO.Directory.GetDirectories(path2, "*");
				if (list != null)
					foreach (string dir in list) {
						foreach (string dll in dlls) {
							string versionDir = dir.Replace(path2, "");
							if (System.IO.File.Exists(dir + @"\" + dll + ".dll") == true)
								if (vers.Contains(versionDir)) {
									vers.Remove(versionDir);
									vers.Add(FileVersionInfo.GetVersionInfo(dir + @"\" + dll + ".dll").ProductVersion.ToString( ));
								} else
									vers.Add(versionDir);
						}
					}
			}

			return vers;
		}

		public string Hardware( ) {
			string vers = "";
			ManagementObjectSearcher searcher;

			searcher = new ManagementObjectSearcher("Select Name from Win32_CDROMDrive");
			foreach (ManagementObject cdrom in searcher.Get( )) {
				vers += "Optic Drive: " + cdrom.GetPropertyValue("Name")
					+ "\n";
			}

			searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
			foreach (ManagementObject cdrom in searcher.Get( )) {
				vers += "Procesor: " + cdrom.GetPropertyValue("Name")
					+ "\n";
			}


			searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
			foreach (ManagementObject cdrom in searcher.Get( )) {
				vers += "HDD: " + cdrom.GetPropertyValue("Model")
					+ "\n";
			}


			searcher = new ManagementObjectSearcher("SELECT * FROM Win32_MemoryDevice");
			foreach (ManagementObject cdrom in searcher.Get( )) {
				vers += "RAM: " + cdrom.GetPropertyValue("Status")
					+ "\n";
			}

			return vers;
		}

		public List<string> InstalledApps( ) {
			List<string> vers = new List<string>( );

			RegistryKey reg = Registry.ClassesRoot;
			reg = reg.OpenSubKey(@"Installer\Products");
			if (reg.GetSubKeyNames( ) != null)
				foreach (string name in reg.GetSubKeyNames( )) {
					string appN = "";
					string appV = "";
					RegistryKey temp = reg.OpenSubKey(name);
					foreach (string s in temp.GetValueNames( )) {
						if (s == "ProductName")
							appN = temp.GetValue("ProductName").ToString( );
						if (s == "Version")
							appV = temp.GetValue("Version").ToString( );
					}
					vers.Add(appN + " Version: " + appV);
				}

			return vers;
		}
	}
}
